package cn.gailvlun.gll.util;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.net.base.Profile;
import io.realm.Realm;
import io.realm.RealmConfiguration;



public class DebugUtil {

    public static final boolean showDebugTokenWindow=false;
    private static Class<ProfileUtil> sProfileUtilClass = null;
    private static Method sGetConfiguration = null;
    private static RealmConfiguration sConfiguration;
    private static Field sToken;

    static {

        sProfileUtilClass = ProfileUtil.class;
        try {
            sToken = sProfileUtilClass.getDeclaredField("token");
            sToken.setAccessible(true);
            sGetConfiguration = sProfileUtilClass.getDeclaredMethod("getConfiguration");
            sGetConfiguration.setAccessible(true);
            sConfiguration = (RealmConfiguration) sGetConfiguration.invoke(sProfileUtilClass);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    public static void debugTokenExpired(Context context, View parent) {
        if (showDebugTokenWindow) {
            
            LinearLayout ll = new LinearLayout(context);
            ll.setOrientation(LinearLayout.VERTICAL);
            final Button token = new Button(context);
            token.setText("token失效");
            token.setBackgroundColor(ContextCompat.getColor(context, R.color.filter_color));
            token.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Realm realm = Realm.getInstance(sConfiguration);
                    if (realm != null) {
                        Profile profile = realm.where(Profile.class).findFirst();
                        realm.beginTransaction();
                        if (profile != null) {
                            profile.setToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Iis4NjE4NTEyMTU3MTE3Iiwib3JpZ19pYXQiOjE1MzAxNjg3MDIsIm1vYmlsZSI6Iis4NjE4NTEyMTU3MTE3IiwiZXhwIjoxNTMwMTkwMzAyLCJ1c2VyX2lkIjozODIsImVtYWlsIjoiIn0.G3PFp3U6HNoapqfJ76s-Mzc8Hu2Zru06MfYrcj_kogQ");
                            try {
                                sToken.set(sProfileUtilClass, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Iis4NjE4NTEyMTU3MTE3Iiwib3JpZ19pYXQiOjE1MzAxNjg3MDIsIm1vYmlsZSI6Iis4NjE4NTEyMTU3MTE3IiwiZXhwIjoxNTMwMTkwMzAyLCJ1c2VyX2lkIjozODIsImVtYWlsIjoiIn0.G3PFp3U6HNoapqfJ76s-Mzc8Hu2Zru06MfYrcj_kogQ");
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }

                            realm.insertOrUpdate(profile);
                            realm.commitTransaction();
                            ToastUtil.showShort("token 已经被篡改为\n" + profile.getToken());
                        } else {
                            ToastUtil.showShort("Profile:" + null);
                        }
                        realm.close();
                    }
                }
            });
            Button rtoken = new Button(context);
            rtoken.setText("refresh token失效");
            rtoken.setBackgroundColor(ContextCompat.getColor(context, R.color.g22));
            rtoken.setTextColor(ContextCompat.getColor(context, R.color.filter_color));
            rtoken.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Realm realm = Realm.getInstance(sConfiguration);
                    if (realm != null) {
                        Profile profile = realm.where(Profile.class).findFirst();
                        realm.beginTransaction();
                        if (profile != null) {
                            profile.setRefresh_token("80bcef0fe6e61e347d56a1c6e6347551bd6b30e4");
                            realm.insertOrUpdate(profile);
                            realm.commitTransaction();
                            ToastUtil.showShort("refresh token已经被篡改为\n" + profile.getRefresh_token());
                        } else {
                            ToastUtil.showShort("Profile:" + null);

                        }
                        realm.close();
                    }
                }
            });
            ll.addView(token);
            ll.addView(rtoken);
            PopupWindow debugTokenWindow = new PopupWindow(ll, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, false);
            debugTokenWindow.setAnimationStyle(R.style.popupwindow_anim_style);
            debugTokenWindow.showAtLocation(parent, Gravity.LEFT, 0, -400);
        }
    }
}
